package com.lyl.servicevod.controller;

import com.lyl.commonutils.R;
import com.lyl.servicevod.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(description="阿里云视频点播微服务")

@RestController
@RequestMapping("/vod/video")
public class VideoAdminController {

    @Autowired
    private VideoService videoService;

    @PostMapping("upload")
    public R uploadVideo(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file) throws Exception {

        String videoId = videoService.uploadVideo(file);
        return R.ok().message("视频上传成功").data("videoId", videoId);
    }

    @PostMapping("getVideoUrl/{vid}")
    public R uploadVideo(
         @PathVariable("vid")  String vid) throws Exception {
        System.out.println("vid:"+vid);
        String url = videoService.getUrlByVid(vid);
        return R.ok().message("获取成功").data("url", url);
    }

    @DeleteMapping("{videoId}")
    public R removeVideoById(
            @PathVariable("videoId")  String vid ) throws Exception {
        System.out.println("vid:"+vid);
       boolean isSuccess = videoService.removeVideoById(vid);// 删除成功之后 把小节资源id置空

       if(isSuccess) {
           return R.ok().message("删除成功");
       }else{
           return R.error().message("删除失败");
       }
    }


    /**
     * 批量删除视频
     * @param videoIdList
     * @return
     */
    @DeleteMapping("delete-batch")
    public R removeVideoList(
            @ApiParam(name = "videoIdList", value = "云端视频id", required = true)
            @RequestParam("videoIdList") List<String> videoIdList){
        System.out.println("批量删除视频"+videoIdList);

       if( videoService.removeVideoList(videoIdList)) {
           return R.ok().message("视频删除成功");
       }
       return  R.error().message("删除失败,请稍后再试");
    }
}